
package inf3791.calendarsystem.others.panels.patients;

import eu.mpower.framework.security.types.soap.SecurityToken;
import inf3791.calendarsystem.shared.AuthenticationChangedListener;
import inf3791.calendarsystem.shared.AuthenticationProvider;

/**
 *
 * @author Jonas Weber
 */
public class PatientsPanel extends javax.swing.JPanel implements AuthenticationChangedListener {

    PatientTableModel patmodel;
    private AuthenticationProvider authprov;

    public AuthenticationProvider getAuthenticationProvider() {
        return authprov;
    }

    public void setAuthenticationProvider(AuthenticationProvider authprov) {
        if (this.authprov != null) {
            this.authprov.removeAuthenticationChangedListener(this);
        }

        this.authprov = authprov;

        if (authprov != null) {
            authprov.addAuthenticationChangedListener(this);
        } else {
            labelHeading.setText("No authentication provider supplied");
        }
    }

    public synchronized void assureDataPresent(SecurityToken sectoken) {
        if (patmodel.patients == null) {
            forceRefresh(sectoken);
        }
    }

    public synchronized void forceRefresh(SecurityToken sectoken) {
        patmodel.forceRefresh(sectoken);
        labelHeading.setText("Patients of you");
    }

    /**
     * Creates new form PatientsPanel
     */
    public PatientsPanel() {
        initComponents();
        patmodel = (PatientTableModel) tabPatients.getModel();
        tabPatients.setDefaultRenderer(Object.class, new PatientRenderer());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
        tabPatients = new javax.swing.JTable();
        labelHeading = new javax.swing.JLabel();

        jScrollPane2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        tabPatients.setModel(new PatientTableModel());
        tabPatients.setRowHeight(15);
        tabPatients.setTableHeader(null);
        jScrollPane2.setViewportView(tabPatients);

        labelHeading.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
        labelHeading.setText("Patients of {0}");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 656, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelHeading)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelHeading)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel labelHeading;
    private javax.swing.JTable tabPatients;
    // End of variables declaration//GEN-END:variables

    @Override
    public void securityTokenChanged(final SecurityToken newToken) {
        
        new Thread() {
            @Override
            public void run() {
                forceRefresh(newToken);
            }
        }.start();
        
    }

    @Override
    public void securityTokenVoided() {
        patmodel.empty();
        labelHeading.setText("Authentication void");

    }
}
